using Engine;
using Engine.Graphics;
using Game;
using Microsoft.VisualBasic.CompilerServices;

namespace SCIENEW {
    /// <summary>
    /// 金属板
    /// </summary>
    public class PlateItem : MeshItem, IMaterialBlock {
        protected BoundingBox[] m_collisionBoxes;
        public Materials Material;
        public bool isSmall = false;
        public Materials GetMaterial() => Material;
        /// <summary>
        /// 非特定材料的金属板，由这个构造
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="color"></param>
        /// <param name="small"></param>
        /// <param name="isCopper"></param>
        public PlateItem(string id, Color color, bool small = false, bool isCopper = false) : base(id) {
            Material = Materials.None;
            m_standaloneBlockMesh.AppendMesh("Models/Ingots", "IronPlate", small ? Matrix.CreateScale(.5f) : Matrix.Identity, Matrix.Identity, color);
            m_collisionBoxes = new BoundingBox[] { m_standaloneBlockMesh.CalculateBoundingBox() };

            Matrix tcTransform = isCopper ? Matrix.CreateTranslation(2 / 16f, 1 / 16f, 0) : Matrix.CreateTranslation(2 / 16f, 0, 0);//是否为铜，是则用铜材质
            m_standaloneBlockMesh.TransformTextureCoordinates(tcTransform);
        }

        /// <summary>
        /// 特定材料金属板，由这个构造
        /// </summary>
        /// <param name="type"></param>
        /// <param name="small"></param>
        public PlateItem(Materials type, bool small = false) : this(type + "Plate", type == Materials.Copper ? Engine.Color.White : MaterialsUtils.GetColor(type), small, type == Materials.Copper) {
            Material = type;
            string name = type.ToDisplayName();
            isSmall = small;
            if (small)
            {
                Id = type.ToString() + "Sheet";
                DisplayName = string.Format(LanguageControl.Get("Items", "Names", "SheetItem"), name);
                Description = string.Format(LanguageControl.Get("Items", "Descriptions", "SheetItem"), name);
                return;
            }
            DisplayName = string.Format(LanguageControl.Get("Items", "Names", "PlateItem"), name);
            Description = string.Format(LanguageControl.Get("Items", "Descriptions", "PlateItem"), name);
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawMeshBlock(primitivesRenderer, m_standaloneBlockMesh, IndustrialModLoader.IETexture, color, size * 1.5f, ref matrix, environmentData);
        }

        public override string GetCraftingId() => Id;


        public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value) => m_collisionBoxes;

        public override CraftingRecipe[] GenerateProceduralRecipe(int value) {
            if (Material != Materials.None) {
                if (isSmall) {
                    //板到片
                    CraftingRecipe plateToSheet = new CraftingRecipe {
                        Description = $"将{Material.ToDisplayName()}板切成薄片",
                        RequiredHeatLevel = ComponentExCraftingTable.RequiredHeatLevel,
                        Ingredients = new string[36],
                        ResultValue = ItemsBlockManager.GetItemID(Material + "Sheet"),//获取片ID
                        ResultCount = 36,
                        RequiredPlayerLevel = 0
                    };
                    plateToSheet.Ingredients[0] = IECraftingRecipesManager.BlockToIngredient(ItemsBlockManager.GetItemIdByMaterial<PlateItem>(Material));
                    return [plateToSheet];
                }
                //锭到板
                CraftingRecipe ingotToPlate = new CraftingRecipe {
                    Description = $"用压板机将{Material.ToDisplayName()}锭压制成{Material.ToDisplayName()}板",
                    RequiredHeatLevel = ComponentPresser.RequiredHeatLevel,
                    Ingredients = new string[36],
                    ResultValue = ItemsBlockManager.GetItemIdByMaterial<PlateItem>(Material),
                    ResultCount = 1,
                    RequiredPlayerLevel = 0
                };
                ingotToPlate.Ingredients[0] = IECraftingRecipesManager.BlockToIngredient(ItemsBlockManager.GetItemIdByMaterial<MetalIngotItem>(Material));
                return [ingotToPlate];
            }
            return base.GenerateProceduralRecipe(value);
        }
    }
}